package com.eningqu.aipen.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.eningqu.aipen.MyApplication;

/**
 * Package:com.eningqu.aipen.utils
 * Author:starr
 * Time:2023/9/14  13:46
 * Description: 封装SharedPreferences
 */
public class SPUtil {

    private static SPUtil spUtilInstance = null;
    private static final String DEFAULT_MODEL_NAME = "DotPen";
    private SharedPreferences preferences = MyApplication.mContext.getSharedPreferences(DEFAULT_MODEL_NAME, Context.MODE_PRIVATE);
    private SharedPreferences.Editor editor = preferences.edit();

    private SPUtil() {
    }

    public static SPUtil getInstance() {
        if (spUtilInstance == null) {
            synchronized (SPUtil.class) {
                if (spUtilInstance == null) {
                    spUtilInstance = new SPUtil();
                }
            }
        }
        return spUtilInstance;
    }

    /**
     * 存数据
     * @param key
     * @param value
     */
    public void putData( String key, Object value) {
        putData(DEFAULT_MODEL_NAME, key, value);
    }

    private void putData( String defaultModelName, String key, Object value) {
        if (value instanceof Boolean) {
            editor.putBoolean(key, (Boolean) value);
        }else if(value instanceof Integer){
            editor.putInt(key, (Integer) value);
        } else if (value instanceof Float) {
            editor.putFloat(key, (Float) value);
        } else if (value instanceof Long) {
            editor.putLong(key, (Long) value);
        } else if (value instanceof String) {
            editor.putString(key, (String) value);
        } else {
            return;
        }
        editor.apply();
    }

    /**
     * 取数据
     * @param key
     * @param defaultValue
     * @return
     */
    public Object getData( String key, Object defaultValue) {
        return getData(DEFAULT_MODEL_NAME, key, defaultValue);
    }

    private Object getData(String defaultModelName, String key, Object defaultValue) {
        if (defaultValue instanceof Boolean) {
            return preferences.getBoolean(key, (Boolean) defaultValue);
        } else if (defaultValue instanceof Integer) {
            return preferences.getInt(key, (Integer) defaultValue);
        } else if (defaultValue instanceof Float) {
            return preferences.getFloat(key, (Float) defaultValue);
        } else if (defaultValue instanceof Long) {
            return preferences.getLong(key, (Long) defaultValue);
        } else if (defaultValue instanceof String) {
            return preferences.getString(key, (String) defaultValue);
        } else {
            return null;
        }
    }

    /**
     * 清空
     */
    public void removeAll(){
        if(editor!=null){
            editor.clear();
            editor.commit();
        }
    }

    /**
     * 删除指定key-value
     * @param key
     * @return
     */
    public boolean remove(String key){
        try{
            editor.remove(key);
            editor.commit();
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
